//
//  FTAvatarHeaderView.m
//  FaceTo
//
//  Created by 姚冬元 on 2017/8/11.
//  Copyright © 2017年 chengbangming. All rights reserved.
//

#import "FTAvatarHeaderView.h"

#define  WaterMaxHeight 150

@implementation FTAvatarHeaderView

- (instancetype)init
{
    self = [super init];
    
    if(self) {
        [self initUI];
    }
    return self;
}

- (void)initUI
{
    self.backgroundColor = FTColor;
    
    _bgimg = [[UIImageView alloc] init];
    _bgimg.image = [self createImageWithColor:FTColor frame:CGRectMake(0, 0, kSCREEN_WIDTH, 150)];
    [self addSubview:_bgimg];
    
    waterHeight = (1-0.1) * WaterMaxHeight;
    
    /// 第一层水波纹view ===========================
    self.waveView = [[UIView alloc] init];
    self.waveView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, WaterMaxHeight);
    [self addSubview:self.waveView];
    self.waveView.backgroundColor = [UIColor colorWithRed:255/255.0f green:255/255.0f  blue:255/255.0f  alpha:0.8];
    
    /// 第二层水波纹 view =======================
    self.waveView2 = [[UIView alloc] init];
    self.waveView2.frame = CGRectMake(0, 0, kSCREEN_WIDTH, WaterMaxHeight);
    [self addSubview:self.waveView2];
    self.waveView2.backgroundColor = [UIColor colorWithRed:255/255.0f  green:255/255.0f  blue:255/255.0f  alpha:0.7];
    
    self.waterTimer = [NSTimer scheduledTimerWithTimeInterval:0.18 target:self selector:@selector(waterAction) userInfo:nil repeats:YES];
    self.waterTimer2 = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(waterAction2) userInfo:nil repeats:YES];
    
    CGFloat avatarSize = iphone6p || iphone6 ? 70 : 60;
    
    _avatar = [[UIImageView alloc] init];
    _avatar.image = [UIImage imageNamed:@"Icon-120"];
    _avatar.layer.cornerRadius = avatarSize/2;
    _avatar.layer.masksToBounds = YES;
    [self addSubview:_avatar];
    
    [_avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.offset(20);
        make.height.mas_equalTo(avatarSize);
        make.width.mas_equalTo(avatarSize);
    }];
}

- (void)waterAction
{
    CGMutablePathRef wavePath = CGPathCreateMutable();
    CGPathMoveToPoint(wavePath, nil, 0,-WaterMaxHeight*0.5);
    float y = 0;
    _horizontal += 0.25;
    for (float x = 0; x <= self.waveView.frame.size.width; x++) {
        //峰高* sin(x * M_PI / self.frame.size.width * 峰的数量 + 移动速度)
        y = 10 * sin(x * M_PI / self.waveView.frame.size.width * 2 - _horizontal) ;
        CGPathAddLineToPoint(wavePath, nil, x, y+waterHeight);
    }
    CGPathAddLineToPoint(wavePath, nil, self.waveView.frame.size.width , WaterMaxHeight*0.5);
    CGPathAddLineToPoint(wavePath, nil, self.waveView.frame.size.width, WaterMaxHeight);
    CGPathAddLineToPoint(wavePath, nil, 0, WaterMaxHeight);
    [self.waveView setShape:wavePath];
}

- (void)waterAction2
{
    CGMutablePathRef wavePath2 = CGPathCreateMutable();
    CGPathMoveToPoint(wavePath2, nil, 0,-WaterMaxHeight*0.5);
    float y2 = 0;
    _horizontal2 += 0.5;
    for (float x2 = 0; x2 <= self.waveView2.frame.size.width; x2++) {
        //峰高* sin(x * M_PI / self.frame.size.width * 峰的数量 + 移动速度)
        y2 = -7* cos(x2 * M_PI / self.waveView2.frame.size.width * 2 + _horizontal2) ;
        CGPathAddLineToPoint(wavePath2, nil, x2, y2+waterHeight);
    }
    CGPathAddLineToPoint(wavePath2, nil, self.waveView2.frame.size.width , WaterMaxHeight*0.5);
    CGPathAddLineToPoint(wavePath2, nil, self.waveView2.frame.size.width, WaterMaxHeight);
    CGPathAddLineToPoint(wavePath2, nil, 0, WaterMaxHeight);
    [self.waveView2 setShape:wavePath2];
}

- (UIImage *)createImageWithColor:(UIColor *)color frame:(CGRect)rect
{
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}


@end
